#include <iostream>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
using std::cout;
using std::endl;
using namespace log4cpp;

void test0(){
    //1.创建布局对象
    PatternLayout * ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn3 = new PatternLayout();
    ptn3->setConversionPattern("%d %c [%p] %m%n");

    //2.创建目的地对象
    OstreamAppender* appender1 = new OstreamAppender("output",&cout);
    //目的地绑定布局
    appender1->setLayout(ptn1);

    FileAppender * appender2 = new FileAppender("fileApp","wd.log");
    appender2->setLayout(ptn2);//目的地与布局一对一进行绑定

    auto appender3 = new RollingFileAppender("rolling",
                                             "ROLL.log",
                                             5 * 1024,
                                             9); 
    appender3->setLayout(ptn3);



    //3.创建记录器
    /* Category & sub1 = Category::getRoot(); */
    Category & sub1 = Category::getRoot().getInstance("sub1");
    //4.设置系统的优先级
    sub1.setPriority(Priority::DEBUG);
    //5.记录器添加目的地
    sub1.addAppender(appender1);
    sub1.addAppender(appender2);
    sub1.addAppender(appender3);

    //6.写日志
    int count = 0;
    while(count < 30){
        sub1.emerg("this is an emerg msg");
        sub1.fatal("this is a fatal msg");
        sub1.alert("this is an alert msg");
        sub1.crit("this is a crit msg");
        sub1.error("this is an error msg");
        sub1.warn("this is a warn msg");
        sub1.notice("this is a notice msg");
        sub1.info("this is an info msg");
        sub1.debug("this is a debug msg");
        count++;
    }

    //7.关闭资源
    Category::shutdown();

    //不要这样使用
    /* delete ptn1; */
}

int main(void){
    test0();
    return 0;
}
